|
1
|
|
|
|
|
2
|
|
|
import {DatabaseInterface, ModelInterface, ModelStaticInterface} from "../JeloquentInterfaces"; |
|
3
|
|
|
import Collection from "./Collection"; |
|
4
|
|
|
import Table from "./Table"; |
|
5
|
|
|
|
|
6
|
|
|
export default class Database implements DatabaseInterface { |
|
7
|
|
|
|
|
8
|
|
|
private _name: string; |
|
9
|
|
|
|
|
10
|
|
|
private _tables: Map<string, Table>; |
|
11
|
|
|
|
|
12
|
|
|
/** |
|
13
|
|
|
* |
|
14
|
|
|
* @param name |
|
15
|
|
|
* @param models |
|
16
|
|
|
*/ |
|
17
|
|
|
constructor(name, models:Array<ModelStaticInterface>) { |
|
18
|
|
|
this._name = name; |
|
19
|
|
|
this._tables = new Map(); |
|
20
|
|
|
|
|
21
|
|
|
models.forEach((model: ModelStaticInterface) => { |
|
22
|
|
|
this.register(model); |
|
23
|
|
|
}); |
|
24
|
|
|
} |
|
25
|
|
|
|
|
26
|
|
|
get name(): string { |
|
27
|
|
|
return this._name; |
|
28
|
|
|
} |
|
29
|
|
|
|
|
30
|
|
|
addIndex(table:string, indexName:string, lookUpKey:string, id:string|number): void { |
|
31
|
|
|
this.table(table).addIndex(indexName, lookUpKey, id) |
|
32
|
|
|
} |
|
33
|
|
|
|
|
34
|
|
|
all(table): Collection { |
|
35
|
|
|
return this.table(table).all(); |
|
36
|
|
|
} |
|
37
|
|
|
|
|
38
|
|
|
allModels(table): Map<string|number, ModelInterface> { |
|
39
|
|
|
return this.table(table).allModels(); |
|
40
|
|
|
} |
|
41
|
|
|
|
|
42
|
|
|
delete(table:string, id:number|string): void { |
|
43
|
|
|
this.table(table).delete(id); |
|
44
|
|
|
} |
|
45
|
|
|
|
|
46
|
|
|
drop(table: string): void { |
|
47
|
|
|
this._tables.delete(table); |
|
48
|
|
|
} |
|
49
|
|
|
|
|
50
|
|
|
find(table:string, id:number|string|Array<string|number>): Collection<ModelInterface>|ModelInterface|null { |
|
51
|
|
|
return this.table(table).find(id); |
|
52
|
|
|
} |
|
53
|
|
|
|
|
54
|
|
|
getIndexByKey(table: string, indexName: string): Map<string|number, Set<string|number>> { |
|
55
|
|
|
return this.table(table).getIndexByKey(indexName); |
|
56
|
|
|
} |
|
57
|
|
|
|
|
58
|
|
|
ids(table: string): Array<string|number> { |
|
59
|
|
|
return this.table(table).ids; |
|
60
|
|
|
} |
|
61
|
|
|
|
|
62
|
|
|
indexes(table: string): Map<string, Map<string|number, Set<string|number>>> { |
|
63
|
|
|
return this.table(table).indexes; |
|
64
|
|
|
} |
|
65
|
|
|
|
|
66
|
|
|
insert(table: string, model: ModelInterface): void { |
|
67
|
|
|
this.table(table).insert(model); |
|
68
|
|
|
} |
|
69
|
|
|
|
|
70
|
|
|
/** |
|
71
|
|
|
* @todo Build better way of parsing queries; |
|
72
|
|
|
*/ |
|
73
|
|
|
query(sql) { |
|
74
|
|
|
const sqlParts = sql.match(/^((SELECT)|(INSERT)|(DELETE))\s+(.*)\s+FROM\s+([^\s]+)(\s+WHERE\s+([^\s]+)\s+(=)\s+([^\s+]))?((\s+)|;)?$/i); |
|
75
|
|
|
|
|
76
|
|
|
if (sqlParts.length === 0) { |
|
77
|
|
|
return null; |
|
78
|
|
|
} |
|
79
|
|
|
|
|
80
|
|
|
const action = sqlParts[1]; |
|
81
|
|
|
//const fields = sqlParts[5].split(','); |
|
82
|
|
|
const table = sqlParts[6] |
|
83
|
|
|
const matchField = sqlParts[8]; |
|
84
|
|
|
const matchValue = sqlParts[10]; |
|
85
|
|
|
|
|
86
|
|
|
if (matchField === 'id') { |
|
87
|
|
|
return this[action.toLowerCase()](table, matchValue); |
|
88
|
|
|
} |
|
89
|
|
|
|
|
90
|
|
|
if (matchField === undefined && action === 'SELECT') { |
|
91
|
|
|
return this.all(table); |
|
92
|
|
|
} |
|
93
|
|
|
|
|
94
|
|
|
return null; |
|
95
|
|
|
} |
|
96
|
|
|
|
|
97
|
|
|
register(model: ModelStaticInterface) { |
|
98
|
|
|
const table = new Table(model); |
|
99
|
|
|
this._tables.set(table.name, table); |
|
100
|
|
|
} |
|
101
|
|
|
|
|
102
|
|
|
registerIndex(table: string, name: string): void { |
|
103
|
|
|
this.table(table).registerIndex(name); |
|
104
|
|
|
} |
|
105
|
|
|
|
|
106
|
|
|
removeIndex(table: string, indexName: string, lookUpKey: string, id:string|number): void { |
|
107
|
|
|
this.table(table).removeIndex(indexName, lookUpKey, id); |
|
108
|
|
|
} |
|
109
|
|
|
|
|
110
|
|
|
select(table: string, id:number|string): ModelInterface { |
|
111
|
|
|
return this.table(table).select(id); |
|
112
|
|
|
} |
|
113
|
|
|
|
|
114
|
|
|
setIndexes(): void { |
|
115
|
|
|
this._tables.forEach((table) => { |
|
116
|
|
|
table.setupIndexes(); |
|
117
|
|
|
}); |
|
118
|
|
|
} |
|
119
|
|
|
|
|
120
|
|
|
showTables(): Array<string> { |
|
121
|
|
|
return [...this._tables.keys()]; |
|
122
|
|
|
} |
|
123
|
|
|
|
|
124
|
|
|
truncate(table: string): void { |
|
125
|
|
|
this.table(table).truncate(); |
|
126
|
|
|
} |
|
127
|
|
|
|
|
128
|
|
|
update(table: string, model: ModelInterface): void { |
|
129
|
|
|
this.table(table).update(model); |
|
130
|
|
|
} |
|
131
|
|
|
|
|
132
|
|
|
private table(name:string): Table { |
|
133
|
|
|
return this._tables.get(name); |
|
134
|
|
|
} |
|
135
|
|
|
} |